package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.TruyenKiemHiepDao;
import com.truyen.persistence.entity.TacGia;
import com.truyen.persistence.entity.ChapterTam;
import com.truyen.persistence.entity.ChapterTruyenKiemHiep;
import com.truyen.persistence.entity.ChapterTmpTruyenKiemHiep;
import com.truyen.persistence.entity.TruyenKiemHiep;
import com.truyen.service.ChapterTamService;
import com.truyen.service.ChapterTruyenKiemHiepService;
import com.truyen.service.ChapterTmpTruyenKiemHiepService;
import com.truyen.service.TruyenKiemHiepService;
import com.truyen.utils.PagingInfo;


@Service
public class TruyenKiemHiepServiceImpl implements TruyenKiemHiepService {
	
	@Autowired
	private TruyenKiemHiepDao truyenKiemHiepDao;
	
	@Autowired
	private ChapterTruyenKiemHiepService chapterTruyenKiemHiepService;
	
	@Autowired
	private ChapterTmpTruyenKiemHiepService chapterTmpTruyenKiemHiepService;
	
	@Autowired
	private ChapterTamService chapterTamService;

	@Override
	public void save(TruyenKiemHiep entity) {
		truyenKiemHiepDao.save(entity);
	}

	@Override
	public TruyenKiemHiep findById(Long id) {
		return truyenKiemHiepDao.findById(id);
	}

	@Override
	public List<TruyenKiemHiep> getAll() {
		return truyenKiemHiepDao.findAll();
	}

	@Override
	public TruyenKiemHiep findByProperty(String column, Object value) {
		List<TruyenKiemHiep> lst = truyenKiemHiepDao.findByProperty(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<TruyenKiemHiep> getLstComicLinkImageNullBlogTruyen(int sourceBy){
		return truyenKiemHiepDao.getLstComicLinkImageNullBlogTruyen(sourceBy);
	}

	@Override
	@Cacheable(cacheName="truyenKiemHiepCacheDataController")
	public List<TruyenKiemHiep> getAllLst(String kwSearch, String sort, String sortBy,	PagingInfo pInfo) {
		return truyenKiemHiepDao.getAllLst(kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public void setHot(Long id) {
		try{
			TruyenKiemHiep entity = findById(id);
			boolean isHot = entity.getIsHot();
			if(isHot){
				entity.setIsHot(false);
			}else{
				entity.setIsHot(true);
			}
			truyenKiemHiepDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void setIsDelete(Long id) {
		try{
			TruyenKiemHiep entity = findById(id);
			boolean isDelete = entity.getIsDelete();
			if(isDelete){
				entity.setIsDelete(false);
			}else{
				entity.setIsDelete(true);
			}
			truyenKiemHiepDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	@Override
	@Cacheable(cacheName="lstTruyenKiemHiepCacheTruyenController")
	public List<TruyenKiemHiep> getListTruyenBy(String orderByColumn,int begin, int max) {
		return truyenKiemHiepDao.getListTruyenBy(orderByColumn,begin,max);
	}

	@Override
	public List<TruyenKiemHiep> getListTruyenRandom(int maxLimit) {
		return truyenKiemHiepDao.getListTruyenRandomBy(maxLimit);
	}

	@Override
	public void updateTotalChapterr(Long id, float totalChapter) {
		try{
			TruyenKiemHiep entity = findById(id);
			entity.setTotalChapter(totalChapter);
			truyenKiemHiepDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<TruyenKiemHiep> getLstTruyenBySourceBy(int sourceBy) {
		return truyenKiemHiepDao.getLstTruyenBySourceBy(sourceBy);
	}

	@Override
	public void updateViews(Long id, long views, long viewsWeek, long viewsMonth) {
		try{
			TruyenKiemHiep entity = findById(id);
			entity.setViews(views);
			entity.setViewsWeek(viewsWeek);
			entity.setViewsMonth(viewsMonth);
			truyenKiemHiepDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="lstTruyenKiemHiepTimKiemCacheTruyenController")
	public List<TruyenKiemHiep> getLstByProperty(String column, Object value, int begin, int maxLimit) {
		return truyenKiemHiepDao.getLstByProperty(column,value, begin, maxLimit);
	}

	@Override
	public List<Object> getObjectByNameUnsigned(String kwSearch, int max, String typeSearch) {
		return truyenKiemHiepDao.getObjectByNameUnsigned(kwSearch,max,typeSearch);
	}

	@Override
	public List<TruyenKiemHiep> getLstByPropertyWithPaging(String column, String value, String sortBy, PagingInfo pInfo) {
		return truyenKiemHiepDao.getLstByPropertyWithPaging(column, value, sortBy, pInfo);
	}

	@Override
	public void updateLiked(Long idTruyen, long liked) {
		try{
			TruyenKiemHiep entity = findById(idTruyen);
			entity.setLiked(liked);
			truyenKiemHiepDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void delete(Long id) {
		try{
			TruyenKiemHiep entity = findById(id);
			List<ChapterTam> lstChapterTam = chapterTamService.getByTruyen(entity);
			if(lstChapterTam != null){
				int sizeList = lstChapterTam.size();
				for(int i=0;i<sizeList;i++){
					ChapterTam chapterTam = lstChapterTam.get(i);
					chapterTamService.delete(chapterTam.getId());
				}
			}
			List<ChapterTmpTruyenKiemHiep> lstchapterTmpTruyenKiemHiep = chapterTmpTruyenKiemHiepService.getByTruyen(entity);
			if(lstchapterTmpTruyenKiemHiep != null){
				int sizeList = lstchapterTmpTruyenKiemHiep.size();
				for(int i=0;i<sizeList;i++){
					ChapterTmpTruyenKiemHiep chapterTmpTruyenKiemHiep = lstchapterTmpTruyenKiemHiep.get(i);
					ChapterTruyenKiemHiep chapterTruyenKiemHiep = chapterTmpTruyenKiemHiep.getChapterTruyenKiemHiep();
					chapterTruyenKiemHiepService.delete(chapterTruyenKiemHiep.getId());
					chapterTmpTruyenKiemHiepService.delete(chapterTmpTruyenKiemHiep.getId());
				}
			}
			truyenKiemHiepDao.delete(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void updateColumnString(long id, String column, String value) {
		try {
			TruyenKiemHiep entity = findById(id);
			if(column.equals("linkImage")){
				entity.setLinkImage(value);
			}
			truyenKiemHiepDao.merge(entity);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="lstTruyenKiemHiepOrderByCacheTruyenController")
	public List<TruyenKiemHiep> getLstByPropertyWithOrderByPaging(String orderBy, PagingInfo pInfo) {
		return truyenKiemHiepDao.getLstByPropertyWithOrderByPaging(orderBy,pInfo);
	}

	@Override
	public List<TruyenKiemHiep> getLstByTacGiaWithPaging(TacGia tacGia, String orderBy, PagingInfo pInfo) {
		return truyenKiemHiepDao.getLstByTacGiaWithPaging(tacGia,orderBy,pInfo);
	}

	@Override
	public List<TruyenKiemHiep> getLstByTacGiaLimit(TacGia tacGia, int begin, int maxLimit) {
		return truyenKiemHiepDao.getLstByTacGiaLimit(tacGia, begin, maxLimit);
	}

	@Override
	public void update(Long idTruyen, String name, String alias,
			String linkImage, String nameImage, String info, TacGia tacGia,
			String categories, float totalChapter, boolean isDelete,
			boolean isFinished, boolean isHot, long liked, long views,
			long viewsWeek, long viewsMonth, int sourceBy,
			String linkSource, String translator) {
		try {
				TruyenKiemHiep entity = findById(idTruyen);
				if(entity != null){
					entity.setName(name);
					entity.setAlias(alias);
					entity.setLinkImage(linkImage);
					entity.setNameImage(nameImage);
					entity.setInfo(info);
					entity.setTacGia(tacGia);
					entity.setCategories(categories);
					entity.setTotalChapter(totalChapter);
					entity.setIsDelete(isDelete);
					entity.setIsFinished(isFinished);
					entity.setIsHot(isHot);
					entity.setLiked(liked);
					entity.setViews(views);
					entity.setViewsWeek(viewsWeek);
					entity.setViewsMonth(viewsMonth);
					entity.setSourceBy(sourceBy);
					entity.setLinkSource(linkSource);
					entity.setTranslator(translator);
					truyenKiemHiepDao.merge(entity);
				}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Async
	@Override
	public void updateViewsWeek() {
		List<TruyenKiemHiep> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenKiemHiep truyen = lstTruyen.get(i);
				truyen.setViewsWeek(0);
				truyenKiemHiepDao.merge(truyen);
			}
		}
	}

	@Async
	@Override
	public void updateViewsMonth() {
		List<TruyenKiemHiep> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenKiemHiep truyen = lstTruyen.get(i);
				truyen.setViewsMonth(0);
				truyenKiemHiepDao.merge(truyen);
			}
		}
	}

	@Override
	public List<TruyenKiemHiep> findByPropertyNotNull(String property) {
		return truyenKiemHiepDao.findByPropertyNotNull(property);
	}

}
